package fab.graphics 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author fab
	 */
	public class MovieClipsUtils
	{
		static public function launch_reverse(mc:MovieClip, postFunction:Function=null ):void
		{
			mc.addEventListener( Event.ENTER_FRAME, reverse );
			mc.addFrameScript( 0, function():void { stop_clip( mc ) ; if ( postFunction != null ) postFunction(); } );
			mc.prevFrame();
		}
		static public function stop_clip(mc:MovieClip):void
		{
			mc.stop();
			mc.addFrameScript( 0, null );
			mc.removeEventListener( Event.ENTER_FRAME, reverse );
			if ( mc.parent ) mc.parent.removeChild( mc );
		}
		static public function stopAllChildren(mc:*, frame:int=1):void
		{
			if ( mc is DisplayObjectContainer )
			for ( var i:int = 0; i < mc.numChildren; i ++ ) 
			{
				stopAllChildren( mc.getChildAt( i ) );
				if ( mc.getChildAt( i ) is MovieClip )
				{
					stopMC( mc.getChildAt( i ) as MovieClip, frame );
				}
			}			
		}		
		
		static public function stopMC(mc:MovieClip, frame:int):void
		{
			mc.gotoAndStop( frame );
		}
		static public function reverse(e:Event):void 
		{
			//if ( (e.currentTarget as MovieClip).currentFrame == 1 ) return stop_clip( e.currentTarget as MovieClip );
			(e.currentTarget as MovieClip).prevFrame();
		}
		
	}

}